<?php
/**
 * Created by allen.qiang
 * Date: 15-6-19
 * Time: 上午10:45
 * 下午茶推广注册页面
 */
quanli_require('module/base/customer/quanliCust.class.php'); //载入会员类
class ajax_tea
{

    public function execute()
    {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: //发送验证码
                    $this->sendSms();
                    break;
                case 2: //立即体验
                    $this->checkCode();
                    break;
                case 3: //根据手机号验证会员信息
                    $this->checkCustMaster();
                    break;


                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {
            $this->toReturn('参数错误');
        }


    }

    /**
     * 发送验证码
     */
    private function sendSms(){
        global $G_SHOP;
        $phone = $_REQUEST['phone']; //手机号码
        //加载短信类
        quanli_include('module/base/common/quanliSms.class.php');

        //实例化短信类
        $quanliSms = new quanliSms();


        if (!isset($phone) || !is_mobile($phone)) {
            returnJsonData('参数错误');
        }


        $current_time = time(); //当前时间戳
        $last_send_time = $G_SHOP->sessionCache_get('sms_yzm_time_' . $phone); //上一次发放短信的时间戳
        if (!empty($last_send_time) && $current_time - $last_send_time < 30) {
            returnJsonData('手机短信已发送，请30s以后再试',400);
        }

        /**
         * ============================
         * 发送短信
         * ============================
         */

        //生成短信验证码
        $sms_yzm = $quanliSms->createSmsCode();

        //$sms_yzm = '1111';
        //生成短信内容
        $data_array = array(
            '$sms_yzm' => $sms_yzm,
            '$mobile' => $phone
        );
        $sms_content = $quanliSms->getSmsContent('dunch_mobile', $data_array);
        if (!$sms_content) {
            returnJsonData('短信初始化失败', 404);
        }

        //发送短信
        $result_array = $quanliSms->sendSMS( $phone, $sms_content );

        /**
         * ============================
         * 发送短信
         * ============================
         */
         if ($result_array['code'] == 200) { //本地环境 注释掉
        //保存短信验证码到redis
        $G_SHOP->sessionCache_set('tea_yzm_' . $phone, md5($sms_yzm), 3600); //将验证码信息保存到memcache中去
        $G_SHOP->sessionCache_set('tea_yzm_time_' . $phone, time(), 3600);

            }
        returnJsonData($result_array, 200);
    }

    /**
     * 立即体验
     */

    private function checkCode(){
        global $G_SHOP;
        $phone=$_REQUEST['phone'];
        $codecon=$_REQUEST['codecon'];

        //检查短信验证码
        $sms_ysm = $G_SHOP->sessionCache_get('tea_yzm_' . $phone);
        if (is_null($sms_ysm)) {
            returnJsonData('短信验证码已失效', 400);
        } elseif ($sms_ysm != md5($codecon)) {
            returnJsonData('短信验证码有误', 400);
        }

        $G_SHOP->sessionCache_remove('tea_yzm_' . $phone);
        $G_SHOP->sessionCache_remove('tea_yzm_time_' . $phone);

        //实例会员类
        $quanliCust = new quanliCust();

        //查询会员信息
        $arg_array = array(
            'custID' => $phone
        );
        $customer_array = $quanliCust->searchCustomerDetail($arg_array);
        //MCAKE会员
        if ($customer_array) {
            $custSql = "select count(1) as usNum from  dunch_cust_master  WHERE pcustID=? ";
            $custArray = $G_SHOP->DBCA->stmt_query($custSql, 'i', $customer_array['pcustID']);
            //MCAKE会员 下午茶会员
            if(isset($custArray) && $custArray[0]->usNum==1){
                setcookie('dunchCookie_custID', $customer_array['custID'], time()+3600*24*7,'/','fistforward.com');//手机号存入cookie
                setcookie('dunchCookie_pcustID', $customer_array['pcustID'], time()+3600*24*7,'/','fistforward.com');//会员ID存入cookie
                //更新会员登录信息
                $quanliCust->shopCheckin( $customer_array, 0 );
                returnJsonData(200, 200);
            }
        }else{
            //非MCAKE会员 先注册
            $pwd = get_random_string(6);
            //前台注册会员信息
            $args_array = array(
                'custID' => $phone,
                'pwd' => $pwd,
                'pwd2' => $pwd,
                'custSource' => 1,
                'custType' => 2
            );
            $result_array = $quanliCust->register($args_array);
            if ($result_array['code'] != 200) {
                returnJsonData($result_array['message'], $result_array['code']);
            }
            //查询会员信息
            $arg_array = array(
                'custID' => $_REQUEST['phone']
            );
            $customer_array = $quanliCust->searchCustomerDetail($arg_array);
        }


        //MCAKE会员 非下午茶会员
        $dunch_cust_master = new entity('dunch_cust_master');
        $dunch_cust_master->pcustID = $customer_array['pcustID'];
        $G_SHOP->DBCA->fetch($dunch_cust_master);
        $dunch_cust_master->pcustID = $customer_array['pcustID'];
        $dunch_cust_master->custID = $customer_array['custID'];
        $dunch_cust_master->brandID = 1;
        $dunch_cust_master->is_coquetry = 1;
        $G_SHOP->DBCA->save($dunch_cust_master);
        setcookie('dunchCookie_custID', $customer_array['custID'], time()+3600*24*7,'/','fistforward.com');//手机号存入cookie
        setcookie('dunchCookie_pcustID', $customer_array['pcustID'], time()+3600*24*7,'/','fistforward.com');//会员ID存入cookie
        //更新会员登录信息
        $quanliCust->shopCheckin($customer_array, 0);
        returnJsonData(202, 200);


    }



}